<?php
   require_once "Model.php";
   require_once "common/connDB.php";
   class IndexModel extends Model{
     
   	 public function __construct(){
        parent::__construct();
   	 }
   	 //添加用户
    public function addUserInfo()
    {
      //使用$_POST数组获取表单中输入的数据
        $sid = trim($_POST['sid']); 
        $sname = $_POST['sname'];
        $password = $_POST['password'];
         
        $sql = "insert into student values('$sid','$sname',md5('$password'))";
        
        $result = $this -> conn -> query($sql);//写入数据库
        return $result;
    }
    //用户登录方法
    public function doLoginInfo()
      {
        $sid = trim($_POST['sid']); 
        $password = $_POST['password'];
        $sql="select * from student where sid='$sid' and password=md5('$password')";
        //echo $sql;
        $result = $this -> conn -> query($sql);
        return $result;
      }
      //管理员登陆方法
      public function doAdminLoginInfo()
      {
        $admin = trim($_POST['admin']); 
        $pw = $_POST['pw'];
        $sql="select * from admin where admin='$admin' and pw=md5('$pw')";
        //echo $sql;
        $result = $this -> conn -> query($sql);
        return $result;
      }
      //查询试卷分类
      public function getTestList()
		{
			
 			$sql = "select * from papers";
 			$data = $this-> conn -> query($sql);
 			return $data;	
		}
		//通过pid查询试卷的题目
		public function getTestByID()
		{
			$pid = $_GET['pid'];
			//echo $pid;
			$sql = "select * from test where pid=$pid";
			//echo $sql;
			$data = $this-> conn -> query($sql);
			return $data;
		}
		//获取测试成绩方法
		public function addTestInfo()
	    {

	      //date_default_timezone_set("Asia/Liaoning");
	      $grade=0;
	      session_start();
	      $name2 = $_SESSION['user'];
	      $sql = "select * from student where sname='$name2'";
	      //echo $sql;
	      $data = $this-> conn -> query($sql);
	      $row2 = $data;
	        //var_dump($row2);
	        $sid = $row2['sid'];
	      
	        $times = date("Y-m-d h:i:sa");
	        //echo $times;
	        //$pid = $_GET['pid'];
	        //echo $pid;
	        $pid = $_POST['pid'];
	        //echo $pid;
	      
	        $sql="select * from test where pid=$pid";
	        //echo $sql;

	        $result = $this -> conn -> query($sql);
	        //var_dump($result);
	   		if($_POST['ans']==$result['ans'])
	   		{
	   			$grade+=$result['score'];
	   		}
	   		//echo $grade;
	   		$tid = $result['tid'];

	        //echo '</br>';
	        //echo $totalprice;
	        //echo '</br>';
	        $sql = "insert into grade values(null,$sid,$pid,'$times',$grade)";
	       //echo $sql;
	        //echo $sql;
	        $result = $this -> conn -> query($sql);//写入数据库
	        $sql = "insert into exam values(null,$pid,$tid)";
	        //echo $sql;
	        $result = $this -> conn ->query($sql);
	        return $result;
	    }
	    //查询成绩方法
	    public function getGradeByName()
	    {
	    	session_start();
	      	$name = $_SESSION['user'];
	      	$sql = "select * from student where sname='$name'";
	      	$result = $this -> conn -> query($sql);
	      	$sid = $result['sid'];
	      	$sql = "select * from grade where sid=$sid";
	      	$result = $this -> conn -> query($sql);
	      	return $result;

	    }
   }

?>